package com.cwf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwf.common.enums.BaseStateEnum;
import com.cwf.common.enums.IsDeleteEnum;

import com.cwf.system.dao.DictionaryMapper;

import com.cwf.system.dao.PermissionDao;
import com.cwf.system.entity.Dictionary;
import com.cwf.system.entity.Permission;
import com.cwf.system.service.DictionaryService;
import com.cwf.system.vo.DictionaryVO;
import com.cwf.system.vo.PermissionTreeVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Dictionary)表服务实现类
 *
 * @author makejava
 * @since 2023-02-19 20:55:54
 */
@Service("dictionaryService")
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    DictionaryService dictionaryService;

    @Autowired
    PermissionDao permissionDao;



//    @Override
//    public PageInfo<Dictionary> dictionaryList(Dictionary dictionary) {
//        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Dictionary::getIsDelete, IsDeleteEnum.NO.getCode());
//
//        List<Dictionary> dictionaryList = dictionaryService.list(queryWrapper);
//
//        PageInfo<Dictionary> of = PageInfo.of(dictionaryList);
//
//
//        return of;
//    }
    @Override
    public PageInfo<DictionaryVO> searchWord(Dictionary dictionary) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(
                    dictionary.getChineseName()!=null,
                    Dictionary::getChineseName,
                            dictionary.getChineseName())
                        .eq(dictionary.getEnglishName()!=null,
                                Dictionary::getEnglishName,
                                dictionary.getEnglishName())

                        .eq(dictionary.getDes()!=null,
                                Dictionary::getDes,
                                dictionary.getDes())

                        .eq(dictionary.getState()!=null,
                                Dictionary::getState,
                                dictionary.getState())
                    .eq(Dictionary::getState, BaseStateEnum.OK.getCode())
            .eq(Dictionary::getIsDelete,IsDeleteEnum.NO.getCode());
        List<Dictionary> list = dictionaryService.list(queryWrapper);

        List<DictionaryVO> collect = list.stream().map(DictionaryVO::toVO).collect(Collectors.toList());

//        List<DictionaryVO> list1 = new ArrayList<>();
//        for (Dictionary dictionary1 : list) {
//            DictionaryVO dictionaryVO = DictionaryVO.toVO(dictionary1);
//            list1.add(dictionaryVO);
//        }

        return PageInfo.of(collect);
    }

    @Override
    public Object addWord(Dictionary dictionary) {

        return dictionaryService.save(dictionary);
    }

    @Override
    public Object updateWord(Dictionary dictionary) {

        return dictionaryService.updateById(dictionary);
    }

    @Override
    public boolean deleteWord(Dictionary dictionary) {

        boolean b = this.updateById(dictionary);

        return b;
    }

//    @Override
//    public List<PermissionTreeVO> treeList() {
//
//        List<Permission> permissions = permissionDao.selectList(null);
//
//        List<PermissionTreeVO> toVOList = PermissionTreeVO.toVOList(permissions);
//
//        Integer integer = toVOList.stream().map(PermissionTreeVO::getRank).min(Comparator.comparing(v -> v)).get();
//
//
//        Map<Long, PermissionTreeVO> collect = toVOList.stream().collect(Collectors.toMap(PermissionTreeVO::getId, v -> v));
//
//        List<PermissionTreeVO> collect1 = toVOList.stream().filter(v -> v.getRank() == 1).collect(Collectors.toList());
//        return toVOList;
//    }


}
